﻿/*
 * 2011-08-29更新：
 * 1.日期默认比首日期多一天
 * 2.结果集实现删除功能
 
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DeliveryApp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        Stimulsoft.Report.StiReport report = new Stimulsoft.Report.StiReport();


        private void button1_Click(object sender, EventArgs e)
        {
            report.Load("Report.mrt");
            report.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            treeView1.ExpandAll();
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            textBox1.Enabled = false;
            //设置dateTimePicker2默认值
            dateTimePicker2.Value = dateTimePicker1.Value.AddDays(1);
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            //panel1.Anchor =
            //    ((System.Windows.Forms.AnchorStyles)
            //     (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom |
            //      System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right));
            
        }

        /// <summary>
        /// treeView节点双击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            
            if(e.Node.Text=="申通")
            {
                report.Load("shentong.mrt");
                ////report.Print();
                ////stiViewerControl1.Report = report;
                ////stiViewerControl1.FirstPage();
                report.Show();
            }
            else if (e.Node.Text == "汇通")
            {
                report.Load("huitong.mrt");
                report.Show();
            }
            else if (e.Node.Text == "圆通")
            {
                report.Load("yuantong.mrt");
                report.Show();
            }
            else if (e.Node.Text == "中通")
            {
                report.Load("zhongtong.mrt");
                report.Show();
            }
            else if (e.Node.Text == "韵达")
            {
                report.Load("yunda.mrt");
                report.Show();
            }
            else if (e.Node.Text == "顺丰")
            {
                report.Load("shunfeng.mrt");
                report.Show();
            }
            else if (e.Node.Text == "宅急送")
            {
                report.Load("zjs.mrt");
                report.Show();
            }
        }

        //DataSet ds = new DataSet();
        DataTable dt = new DataTable();

        /// <summary>
        /// 查询按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btQuery_Click(object sender, EventArgs e)
        {
            //设置值格式
            //MessageBox.Show(dateTimePicker1.Value.ToString("yyyy-MM-dd"));
            
            //Bug:Datatable加载前未清空
            dt.Clear();

            string strsql = "";
            string wStrsql = "";
            string strconnect = "user id=sa; password=baison; Database=xlpt;Server=192.168.158.6\\SQL2005;Connect Timeout=30";
            SqlConnection sqlcon = new SqlConnection(strconnect);

            strsql = "select * from vw_KpDelivery ";

            if(radioButton1.Checked==true)
            {
                wStrsql = "where [开票时间] between '" + dateTimePicker1.Value.ToString("yyyy-MM-dd") + "' and '" + dateTimePicker2.Value.ToString("yyyy-MM-dd") + "'";
                strsql = strsql + wStrsql;
                SqlDataAdapter da = new SqlDataAdapter(strsql ,sqlcon);
                da.Fill(dt);
            }
            else if (radioButton2.Checked==true)
            {
                wStrsql = "where [开票单号]= '" + textBox1.Text.ToString() + "'";
                strsql = strsql + wStrsql;
                SqlDataAdapter da = new SqlDataAdapter(strsql, sqlcon);
                da.Fill(dt);
            }
            else
            {
                MessageBox.Show("请选择条件！");
            }
            dataGridView1.DataSource = dt;
            report.RegData(dt);

        }

        /// <summary>
        /// 模板设计按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btDesign_Click(object sender, EventArgs e)
        {
            report.Design();
        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            radioButton1.Checked = true;
            radioButton2.Checked = false;

            dateTimePicker1.Enabled = true;
            dateTimePicker2.Enabled = true;
            textBox1.Enabled = false;
        }

        private void radioButton2_Click(object sender, EventArgs e)
        {
            radioButton1.Checked = false;
            radioButton2.Checked = true;

            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            textBox1.Enabled = true;
        }

        /// <summary>
        /// DataGridView右键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tmDelete_Click(object sender, EventArgs e)
        {
            for(int i=dataGridView1.SelectedRows.Count;i>0;i--)
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i-1].Index);

            }
            dt.AcceptChanges();
        }


    }
}
